import { GameConfig } from "../configs/GameConfig";
import { ILevelElement } from "../configs/Level";
import { AdsUtil } from "../etc/AdsUtil";
import { LevelModuleC, LevelModuleData } from "../module/LevelModule";
import AutoAdsWindow_Generate from "../ui-generate/AutoAdsWindow_generate";

export class AutoAdsWindow extends AutoAdsWindow_Generate{
    private showSecond = 8
    private currentTime = 0
    private levelData:LevelModuleData
    private currentLevelCfg:ILevelElement

    private _context:string
    private set context(value:string){
        if(value == this._context) return
        this.txt_context.text = value
        this._context = value
    }

    onStart(){
        this.levelData = DataCenterC.getData(LevelModuleData)
        this.btn_close.onClicked.add(this.onCloseClicked.bind(this))
        this.canUpdate = true
    }

    onShow(){
        this.currentTime = 0
        let checkpointCfg = GameConfig.Checkpoint.getElement(this.levelData.checkPointID)
        this.currentLevelCfg = GameConfig.Level.getElement(checkpointCfg.Kind)
    }

    onUpdate(){
        this.currentTime += 0.03
        if(this.currentTime >= this.showSecond){
            this.autoClose()
        }
        this.bar_timer.currentValue = this.currentTime / this.showSecond
        // this.img_effect.position = util.getTemporaryVector2(util.lerp(-100, 700, (TimeUtil.elapsedTime() * 0.5) % 1), -150)
        this.context = `观看广告可以获得${this.currentLevelCfg.AdsRewardMoneyCount}红包\n(${Math.floor(this.showSecond - this.currentTime)}秒后自动播放)`
    }

    // 八秒内玩家未作出选择
    autoClose(){
        UIService.hideUI(this)
        // 播放激励广告
        AdsUtil.playRewardAds(
            // 成功则加金币
            ()=>{
                ModuleService.getModule(LevelModuleC).playLevelAutoAdsSuccess()
            },
            // 失败无回调
            ()=>{}
        )
    }

    private onCloseClicked(){
        UIService.hideUI(this)
    }
}